home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / GNU-SMALLTALK.lha / examples / RandomInteger.st < prev    next >
Text File  |  1992-02-15  |  536b  |  32 lines

  1.  
  2. "By Andy Valencia (uunet!sequent!vandys)"
  3.  
  4. "A source of random integers"
  5.  
  6. Integer subclass: #RandomInteger
  7.     instanceVariableNames: ''
  8.     classVariableNames: 'Source'
  9.     poolDictionaries: ''
  10.     category: nil.
  11. !
  12.  
  13. RandomInteger comment:
  14. 'My instances are random integers'
  15. !
  16.  
  17. !RandomInteger class methodsFor: 'instance creation'!
  18.  
  19. new
  20.     ^ self error: 'Must use between:and:'
  21. !
  22.  
  23. between: low and: high
  24.     | i range |
  25.  
  26.     (Source = nil) ifTrue: [ Source _ Random new ].
  27.     range _ high - low.
  28.     i _ (((Source next) * (range + 1)) + low) rounded.
  29.     ^ i
  30. !!
  31.  
  32.